package com.fg.cloud.factory.controller.agency;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Tree;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.service.agency.AgencyProductService;
import com.google.common.collect.Lists;

/**
 * 经销商-订货权限controller
 * @author around
 * @date 2017-6-28
 */
@Controller
@RequestMapping("/agencyProduct")
public class AgencyProductController extends BaseController {

	@Autowired
	private AgencyProductService agencyProductService;

	/**
	 * 经销商订货权限 -- 厂商端使用
	 * 分页查询 前端必传 ageId，确认经销商对应的权限
	 * @param request
	 * @param session
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param product
	 */
	@RequestMapping(value = "/findList", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, 
			Integer page, Integer rows, String sort, String order,
			Long ageId) {
		if(null == ageId) {
			doResult(response, false, StatusCode.LACKVALUE, null, "经销商信息不能为空");
			return;
		}
		try {
			List<Tree> list = agencyProductService.findList(
					getCurrentFactoryUser().getFacId(), ageId);
			if(null == list) list = Lists.newArrayList();
			doResult(response, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:厂商平台经销商管理订货权限--新增查询经销商订货权限
	 * @param request
	 * @param response
	 * @param ageId
	 * @author around
	 * @date 2017年8月16日下午7:19:39
	 */
	@RequestMapping(value = "/findInsert", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void findInsert(HttpServletRequest request, HttpServletResponse response,
			Long ageId) {
		if(null == ageId) {
			doResult(response, false, StatusCode.LACKVALUE, null, "经销商信息为空");
			return;
		}
		try {
			List<Tree> treeList = agencyProductService.findAddAgencyProductByCategorys(
					getCurrentFactoryUser().getFacId(), ageId);
			doResult(response, true, StatusCode.FINDSUCCESS, treeList, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存经销商订货权限
	 * @param request
	 * @param response
	 * @param originalIds 原始产品分类选中ID集合，前端负责在展现的时候统计
	 * @param categoryIds 新产品分类选中ID集合，以","隔开
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public void saveOrUpdate(HttpServletRequest request, HttpServletResponse response, 
			Long ageId, String originalIds, String categoryIds) {
		try {
			if(StringUtils.isEmpty(categoryIds)) {
				doResult(response, false, StatusCode.SAVEERROR, null, "必须选择分类权限");
				return;
			}
			
			boolean flag = agencyProductService.refreshAgencyProductByCategory(
					getCurrentFactoryUser().getFacId(), ageId, originalIds, categoryIds);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, 
					null, flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			doResult(response, false, StatusCode.SAVEERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 删除信息
	 * @param request
	 * @param response
	 * @param ids String => x,x,x
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deletes", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void deletes(HttpServletRequest request, HttpServletResponse response, 
			Long ageId, String categoryIds) throws IOException {
		if(StringUtils.isEmpty(categoryIds)) {
			doResult(response, false, StatusCode.REMOVEERROR, null, "必须选择分类权限");
			return;
		}
		try {
			boolean flag = agencyProductService.batchDeletes(getCurrentFactoryUser().getFacId(), 
					ageId, LongUtils.convert(categoryIds.split("[, ]+")));

			doResult(response, flag, flag ? StatusCode.REMOVESUCCESS : StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS : ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			e.printStackTrace();
		}
		
	}
}
